<?php

namespace Home\Controller;

class ToiletsController extends CommonController {

    public function getNearbyToilets(){
        if (!isset($_GET['location'])) {
            return $this->ajaxReturn($this->buildResult(false, 'location必填'));
        }

        $Comment = M('Comment');
        $ToiletService = D('Toilet', 'Service');
        $_GET['radius'] = is_numeric($_GET['radius']) ? $_GET['radius'] : 0;
        $result = $ToiletService->getNearbyToiletsFromLBS($_GET['location'], $_GET['radius']);

        foreach ($result['data'] as $key => $toilet) {
            $result['data'][$key]['comment_count'] = $Comment->where("toilet_id={$toilet['map_table_id']}")->count();

            $result['data'][$key]['longitude'] = $toilet['location'][0];
            $result['data'][$key]['latitude'] = $toilet['location'][1];
            unset($result['data'][$key]['location']);
        }

        return $this->ajaxReturn($result);
    }

    public function getToiletById() {
        $Toilet = M('Toilet');
        $Comment = D('Comment');
        $toilet = $Toilet->find($_GET['id']);

        if (empty($toilet)) {
            return $this->ajaxReturn($this->buildResult(false, '指定的toilet不存在'));
        }

        $comments = $Comment->relation(true)->where(array('toilet_id' => $toilet['id']))->select();
        $toilet['comments'] = $comments;

        return $this->ajaxReturn($this->buildResult(true, "成功 {$_GET['id']}", $toilet));
    }

    public function create() {
        if (!isset($_POST['req'])) {
            return $this->ajaxReturn($this->buildResult(false, 'toilet必填'));
        }

        $ToiletService = D('Toilet', 'Service');
        $toilet = json_decode($_POST['req'], true);
        $result = $ToiletService->create($toilet);
        if (false == $result['status']) {
            return $this->ajaxReturn($this->buildResult(false, '添加toilet失败'));
        }

        return $this->ajaxReturn($this->buildResult(true, '添加toilet成功'));
    }

    public function like() {
        if (!isset($_GET['id'])) {
            return $this->ajaxReturn($this->buildResult(false, 'id必填'));
        }

        $Toilet = D('Toilet');
        if (false == $Toilet->where(array('id' => $_GET['id']))->setInc('like_count')) {
            return $this->ajaxReturn($this->buildResult(false, '点赞失败'));
        }

        return $this->ajaxReturn($this->buildResult(true, '点赞成功'));
    }

    public function importDbFromLBS() {
        $ToiletService = D('Toilet', 'Service');
        $result = $ToiletService->getNearbyToiletsFromLBS('113.553087,22.36817', 10000);

        $Toilet = M('Toilet');

        $Toilet->startTrans();
        $toilets = $result['data'];
        foreach ($toilets as $key => $toilet) {
            $toilet['name'] = $toilet['title'];
            $toilet['created_at'] = date('Y-m-d H:i:s', $toilet['create_time']);
            $toilet['updated_at'] = date('Y-m-d H:i:s', $toilet['modify_time']);
            if (false === $Toilet->add($toilet)) {
                $Toilet->rollback();
                return $this->ajaxReturn($this->buildResult(false, '导入失败'));
            }
        }

        $Toilet->commit();
        return $this->ajaxReturn($this->buildResult(true, '导入成功'));
    }
}
